package com.itcodai.controller.course05;

import com.itcodai.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 * @author c-fanqj
 * @date 2023/3/22
 * @apiNote
 */
//@RestController
//@RequestMapping(value = "/test", produces = "application/json; charset=UTF-8")
public class MVCTestController {

    private static final Logger logger =
            LoggerFactory.getLogger(MVCTestController.class);

    /**
     * 测试@RequestMapping
     * @return
     */
    //@RequestMapping(value = "/get", method = RequestMethod.GET)
    //@GetMapping("/get")
    public String testGet() {
        return "success";
    }

    /**
     * 测试@Pathvariable注解，一个参数 GET
     * @param id
     * @return
     */
    //@GetMapping("user/{idd}")
    public String testPathVariable(@PathVariable(value = "idd") Integer id) {
        logger.info("获取到的id为：{}", id);
        return "success";
    }

    /**
     * 测试@Pathvariable注解，多个参数
     * @param id
     * @return
     * http://localhost:8001/test/user/2/zhangsan
     */
    //@GetMapping("/user/{idd}/{name}")
    public String testPathVariable(@PathVariable(value = "idd") Integer id,
                                   @PathVariable String name) {
        logger.info("获取到的id为：{}", id);
        logger.info("获取到的name为：{}", name);
        return "success";
    }

    /**
     * 测试@RequestParam注解 GET POST
     * @param id
     * @return
     * http://localhost:8001/user?id=1
     */
    //@GetMapping("/user")
    public String testRequestParam(@RequestParam Integer id) {
        logger.info("获取到的id为：{}", id);
        return "success";
    }

    // http://localhost:8080/user?idd=1
    /*@RequestMapping("/user")
    public String testRequestParam(@RequestParam(value = "idd", required = false)
                                           Integer id) {
        System.out.println("获取到的id为：" + id);
        return "success";
    }*/

    /**
     * 测试@RequestBody注解: POST JSON
     * @param user
     * @return
     */
    //@PostMapping("/user")
    public String testRequestBody(@RequestBody User user) {
        System.out.println("获取到的username为：" + user.getUsername());
        System.out.println("获取到的password为：" + user.getPassword());
        return "success";
    }

    /**
     * 测试@RequestParam多个参数（表单提交）POST
     * @param username
     * @param password
     * @return
     */
    //@PostMapping("/form1")
    public String testForm(@RequestParam String username,
                           @RequestParam String password) {
        System.out.println("获取到的username为：" + username);
        System.out.println("获取到的password为：" + password);
        return "success";
    }

    /**
     * 测试表单提交，接收实体前不能加@RequestBody注解
     * @param user
     * @return
     */
    //@PostMapping("/form2")
    public String testForm(User user) {
        System.out.println("获取到的username为：" + user.getUsername());
        System.out.println("获取到的password为：" + user.getPassword());
        return "success";
    }

}
